//
//  UIImage+Extension.swift
//  CosClub
//
//  Created by 沙睿 on 2020/3/18.
//  Copyright © 2020 ios. All rights reserved.
//

import Foundation
import UIKit
extension UIImage {
    
    /// 颜色转图片异步
    /// - Parameters:
    ///   - color: 颜色
    ///   - size: 大小 默认CGSize(width: 10, height: 10)
    ///   - cornerRadius: 圆角 默认0
    ///   - stretch: 是否拉伸  默认 false
    ///   - complete: 异步回调
    /// - Returns: Void
    public static func color(_ color: UIColor,
                             size: CGSize = CGSize(width: 10, height: 10),
                             cornerRadius: CGFloat = 0,
                             stretch: Bool = false,
                             complete: ((_ image: UIImage?)->())?) {
        
        DispatchQueue.global().async {
            
            var trueSize = size
            // 判断是否为拉伸类型
            if cornerRadius > trueSize.height/2 && stretch {
                trueSize = CGSize(width: cornerRadius * 2, height: cornerRadius * 2)
            }
            
            let scale = UIScreen.main.scale
            
            let rect: CGRect = CGRect(origin: CGPoint.zero, size: trueSize)
            
            UIGraphicsBeginImageContextWithOptions(rect.size, false, scale)
            
            guard let context: CGContext = UIGraphicsGetCurrentContext() else {
                UIGraphicsEndImageContext()
                return
            }
            
            let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
            path.addClip()
            context.setFillColor(color.cgColor)
            context.addPath(path.cgPath)
            context.fillPath()
            
            var image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            // 拉伸
            if stretch {
                let insets = UIEdgeInsets(top: cornerRadius, left: cornerRadius, bottom: cornerRadius, right: cornerRadius)
                image = image?.resizableImage(withCapInsets: insets, resizingMode: .stretch)
            }
            
            guard let complete = complete else {
                return
            }
            
            DispatchQueue.main.async {
                complete(image)
            }
        }
    }
    
    
    /// 圆角图片
    /// - Parameters:
    ///   - cornerRadius: 圆角曲度
    ///   - size: 裁剪大小 默认为自身大小
    ///   - complete: 异步回调
    /// - Returns: Void

    public func cornerImage(cornerRadius: CGFloat, size: CGSize? = nil, complete: ((_ image: UIImage?)->())?) {
        
        DispatchQueue.global().async {
            
            guard let complete = complete else {
                return
            }
            
            var curSize = size
            if curSize == nil {
                curSize = self.size
            }
            
            guard let size = curSize else {
                return complete(nil)
            }
            
            let scale = UIScreen.main.scale

            UIGraphicsBeginImageContextWithOptions(self.size, false, scale)
            
            let path  = UIBezierPath(roundedRect: CGRect(origin: CGPoint(x: 0, y: 0), size: size),
                                     cornerRadius: cornerRadius)
            path.addClip()
            self.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: size))
            let out = UIGraphicsGetImageFromCurrentImageContext()
            
            UIGraphicsEndImageContext()
            
            DispatchQueue.main.async {
                complete(out)
            }
        }
    }
    
    public func roundImage(complete: ((_ image: UIImage?)->())?) {
        let min = CGFloat.minimum(self.size.width, self.size.height)
        let trueSize = CGSize(width: min, height: min)
        cornerImage(cornerRadius: min/2, size: trueSize, complete: complete)
    }
    
    
    /// 从基础控件获取image
    /// - Parameter named: 图片名称
    /// - Returns: <#description#>
    public static func imageFromBasis(named: String?) -> UIImage?{
        guard let name = named else {
            return nil
        }
        
        let mbundle = Bundle.main
        let path = mbundle.bundlePath + "/Frameworks/RuiBasis.framework/RuiBasis.bundle"
        let bundle = Bundle.init(path: path)
        return UIImage(named: name, in: bundle, compatibleWith: nil)
    }
}
